
import os

env = Environment(ENV = os.environ)
#env = Environment()

def buildit( module ):
    global env

    if os.path.exists( 'SConstruct' ):
        env.SConscript( module + '/SConscript',
                            variant_dir=module + '/obj',
                            duplicate=0 )
    else:
        env.SConscript( module + '/SConscript',
                            variant_dir='../' + module + '/obj',
                            duplicate=0 )
    return


def BuildModules( modules ):
    for mm in modules:
        buildit(mm)

env.buildit = buildit
env.BuildModules = BuildModules


def generate_arm(source, target, env, for_signature):
    return '$OBJCOPY -O binary %s %s'%(source[0], target[0])

env.Append(BUILDERS={
    'Objcopy': Builder(
               generator=generate_arm,
               suffix='.bin',
               src_suffix='.elf')})


env['AR'] = 'arm-none-eabi-ar'
env['AS'] = 'arm-none-eabi-as'
env['CC'] = 'arm-none-eabi-gcc'
env['CXX'] = 'arm-none-eabi-g++'
env['RANLIB'] = 'arm-none-eabi-ranlib'
env['OBJCOPY'] = 'arm-none-eabi-objcopy'

env['PROGSUFFIX'] = '.elf'


env['CPPPATH'] = [
                  '#src/sys',
                  '#src/peripherals'
                 ]

env.Append(INSTALL_LIBDIR = './lib')
env.Append(INSTALL_BINDIR = './bin')
env.Append(CCFLAGS = ['-O2',
                      #'-g',
                      #'-Wall',
                      '-mlittle-endian',
                      '-mthumb',
                      '-mcpu=cortex-m4',
                      '-mthumb-interwork',
                      '-mfloat-abi=soft',
                      '-mfpu=fpv4-sp-d16',
                      '-ffreestanding',
                      '-nostdlib'])
                      
env.Append(LINKFLAGS = ['-Tbuild/stm32_flash.ld',
                        '-mlittle-endian',
                        '-mthumb',
                        '-mcpu=cortex-m4',
                        '-mthumb-interwork',
                        '-mfloat-abi=soft',
                        '-mfpu=fpv4-sp-d16'])
                      
env.Append(LIBPATH = ['#lib'])

Export('env')

BuildModules(['src'])

